
 const {remote, clipboard, ipcRenderer } = require('electron');
 var tempRud=0;
 var tempRaf=false;
 var temp="rud";
 ipcRenderer.on('message-from-main', (event, arg) => {

 
   needinit(JSON.parse(arg));
   rece_fromudp(JSON.parse(arg));
	
	tempRaf=(JSON.parse(arg).RAF);
	tempRud=(JSON.parse(arg).RUD);
	rudShow(tempRaf,tempRud);
   })


//https://canvas-gauges.com/documentation/user-guide/configuration

  ipcRenderer.on('text', (event, arg) => {
      if((arg=="Eng")||(arg=="ENG")){
		temp="rud";
		$('canvas[data-id="radial-one"]').attr('data-title',"Wind Dir");
		$('canvas[data-id="radial-two"]').attr('data-title',"RPM L");
		$('canvas[data-id="radial-three"]').attr('data-title',"RPM R");
		$('canvas[data-id="radial-four"]').attr('data-title',"SPD");
		$('canvas[data-id="radial-five"]').attr('data-title',"Wind S");
		$('canvas[data-id="radial-six"]').attr('data-title',"ROT");
		
		//换图片
		rudShow(tempRaf,tempRud);
		}
      else{
		temp="rud//chn"
		$('canvas[data-id="radial-one"]').attr('data-title',"风向");
		$('canvas[data-id="radial-two"]').attr('data-title',"转速 左");
		$('canvas[data-id="radial-three"]').attr('data-title',"转速 右");
		$('canvas[data-id="radial-four"]').attr('data-title',"速度");
		$('canvas[data-id="radial-five"]').attr('data-title',"风速");
		$('canvas[data-id="radial-six"]').attr('data-title',"转向速率");
		rudShow(tempRaf,tempRud);
		 //换图片
      }
      })
	  
	  
  var hours = 0;
   var minutes = 0;
   var seconds = -1;
  var flage = 0;
  var timeId;
  
  
  function needinit(str)
  {
 if (str.WH == "100")
	 {
	 	window.clearInterval(timeId);
	 	hours = 0;
    	seconds = -1;
	 	flage = 0;
		 tempRud=0;
		 tempRaf=false;
		 temp="rud";
	 	drawScene();
	 }
	 else
	 {
	 	if(flage == 0)//这个时时钟的进入函数  比较特殊  只进入一次
	 	{
	 		hours=parseInt(str.WH);
	 		timeId = setInterval("drawScene()", 1000);
	 		flage=1;
	 	 }
	 }
	

	  
   }

  
  function rpm(str ,str1,str2){// 左  右 故
  
  	if((Number(str)<200)&&(Number(str)>-200))
	{
		changePanel('radial-two','samll');
	}
	else{//换表
		changePanel('radial-two','big');
  	}

	  if(str2 == "TRUE")
	  {
		 $('canvas[data-id="radial-two"]').attr('data-value','0');
	  }else{
		   
		  $('canvas[data-id="radial-two"]').attr('data-value',str);
	  }
	
	if((Number(str1)<200)&&(Number(str1)>-200))
	{
		changePanel('radial-three','samll');
	}
	else{//换表
		changePanel('radial-three','big');
	}
	if(str2 == "TRUE")
	{
		$('canvas[data-id="radial-three"]').attr('data-value','0');
	 }else{
		  
		 $('canvas[data-id="radial-three"]').attr('data-value',str1);
	 }

  }
  function rece_fromudp(obj){
	  
	  //  //故障检测
	    console.log(obj.LRPM);
		console.log(obj.RPM);
		console.log(obj.RPMF);
		console.log(obj.WS);
		console.log(obj.ROT);
	   if((obj.WDF == "TRUE")||(obj.RAF == "TRUE")||(obj.SSF== "TRUE")||(obj.RPMF=="TRUE")){
		   document.body.style.backgroundColor="#380000";
		   
	   }
	   else
	   {
		   
		    document.body.style.backgroundColor="#000000";
	   }
	   
	   
	 if(obj.WDF == "TRUE"){//风向故障
		$('canvas[data-id="radial-one"]').attr('data-value','0');
	 }
	 else{
		$('canvas[data-id="radial-one"]').attr('data-value',obj.WD);
	 }
  rpm(obj.LRPM, obj.RPM ,obj.RPMF);
  
if(obj.SSF== "TRUE")
	{
		$('canvas[data-id="radial-four"]').attr('data-value','0');
	}
	else
	{
	
		$('canvas[data-id="radial-four"]').attr('data-value', obj.SPD);

		
	}
  		
	$('canvas[data-id="radial-five"]').attr('data-value', obj.WS);

  var tempROT;
  		tempROT=obj.ROT;
	  if(Number(tempROT) > 200)
	  {
		tempROT=200;
	  }
	  if(Number(tempROT) < -200)
	  {
		tempROT=-200;
	  }
	 
	  $('canvas[data-id="radial-six"]').attr('data-value', tempROT);
 
 
  }
  function rudShow(raf,rud){
  if(raf == "TRUE")
  	{
  	   		drawWord(0);
  	}   
  	else
  	{
  		drawWord(parseInt(rud));
  	} 
  }

function changePanel(name,size){
	var nameStr="canvas[data-id=" +name+ "]";
	if(size=="big"){

		$(nameStr).attr('data-min-value', '-6000');
		$(nameStr).attr('data-max-value', '6000');
		$(nameStr).attr('data-major-ticks', "[6000,5000,4000,3000,2000,1000,0,1000,2000,3000,4000,5000,6000]");
		$(nameStr).attr('data-highlights','[{"from": -6000, "to": 0, "color": "#ec0a0ac9"},{"from": 0, "to": 6000, "color": "#2dc309"}]');

	}

	else{

		$(nameStr).attr('data-min-value', '-200');
		$(nameStr).attr('data-max-value', '200');
		$(nameStr).attr('data-major-ticks', "[200,150,100,50,0,50,100,150,200]" );
		$(nameStr).attr('data-highlights','[{"from": -200, "to": 0, "color": "#ec0a0ac9"},{"from": 0, "to": 200, "color": "#2dc309"}]');
	
	}




}


  // inner variables
  var canvas, ctx;
  var clockRadius = 230;
  var clockImage;
  
  // draw functions :
  function clear() { // clear canvas function
      ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  }
  
  function drawScene() { // main drawScene function
  

      clear(); // clear canvas
  
      // get current time
      
     
      seconds = seconds + 1;
      hours = hours > 12 ? hours - 12 : hours;
      var hour = hours + minutes / 60;
      var minute = minutes + seconds / 60;
  
      // save current context
      ctx.save();
  
     
	
  
      ctx.translate(canvas.width / 2, canvas.height / 2);
      ctx.beginPath();
  
      // draw numbers
      ctx.font = '40px Arial';
      ctx.fillStyle = '#000';
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      for (var n = 1; n <= 12; n++) {
          var theta = (n - 3) * (Math.PI * 2) / 12;
          var x = clockRadius * 0.6 * Math.cos(theta);
          var y = clockRadius * 0.6 * Math.sin(theta);
          ctx.fillText(n, x, y);
      }
  
      // draw hour
      ctx.save();
      var theta = (hour - 3) * 2 * Math.PI / 12;
      ctx.rotate(theta);
      ctx.beginPath();
      ctx.moveTo(-15, -5);
      ctx.lineTo(-15, 5);
      ctx.lineTo(clockRadius * 0.5, 1);
      ctx.lineTo(clockRadius * 0.5, -1);
      ctx.fill();
      ctx.restore();
  
      // draw minute
      ctx.save();
      var theta = (minute - 15) * 2 * Math.PI / 60;
      ctx.rotate(theta);
      ctx.beginPath();
      ctx.moveTo(-15, -4);
      ctx.lineTo(-15, 4);
      ctx.lineTo(clockRadius * 0.6, 1);
      ctx.lineTo(clockRadius * 0.6, -1);
      ctx.fill();
      ctx.restore();
  
      // draw second
      ctx.save();
      var theta = (seconds - 15) * 2 * Math.PI / 60;
      ctx.rotate(theta);
      ctx.beginPath();
      ctx.moveTo(-15, -3);
      ctx.lineTo(-15, 3);
      ctx.lineTo(clockRadius * 0.75, 1);
      ctx.lineTo(clockRadius * 0.75, -1);
      ctx.fillStyle = '#042d6d';
      ctx.fill();
      ctx.restore();
  
      ctx.restore();
  }
  
  // initialization
  $(function(){
      canvas = document.getElementById('canvas');
      ctx = canvas.getContext('2d');

  drawScene();
 //setTimeout("drawScene()",1000);
 


  });
  
 
  

	 //添加文字方法   更换图片的方法
  		  function drawWord(text){
			  var color;
  			if(text>0)
  			{
  			  		if(text>=35) text=35;		
  			  				document.getElementById('rudshow').innerText=PrefixZero(text, 2);
							document.getElementById("rudshow").style.color = "#00ff00";
							
							if((0<text)&&(text<5))
							  {
								$("#rudimg").css('background-image',"url(img/"+ temp+"/0.png)");  
							  }
							  else if((5<=text)&&(text<10))
							  {
								  $("#rudimg").css('background-image',"url(img/"+ temp+"/g5.png)");
							  }
							  else if ((10<=text)&&(text<15))
							  {
								  $("#rudimg").css('background-image',"url(img/"+ temp+"/g10.png)");
							  }
							  else if ((15<=text)&&(text<20))
							  {
								 $("#rudimg").css('background-image',"url(img/"+ temp+"/g15.png)"); 
							  }
							 else if ((20<=text)&&(text<25))
							 {
								 $("#rudimg").css('background-image',"url(img/"+ temp+"/g20.png)");
							 }
							else if ((25<=text)&&(text<30))
							{
								$("#rudimg").css('background-image',"url(img/"+ temp+"/g25.png)");
							} 
							else if ((30<=text)&&(text<35))
							{
								$("#rudimg").css('background-image',"url(img/"+ temp+"/g30.png)");
							}
							else if (text==35)
							{
								$("#rudimg").css('background-image',"url(img/"+ temp+"/g35.png)");
							}
							
									
  			  				 
  			}
  			else if (text < 0){
				if(text<=-35) text=-35;
  			  				 text =- text;
  			  				
							document.getElementById('rudshow').innerText=PrefixZero(text, 2);
							document.getElementById("rudshow").style.color = "#ff0000";
  			  				if((0<text)&&(text<5))
  			  				  {
								$("#rudimg").css('background-image',"url(img/"+ temp+"/0.png)");  
							  }
  			  				  else if((5<=text)&&(text<10))
  			  				  {
								  $("#rudimg").css('background-image',"url(img/"+ temp+"/r5.png)");
							  }
  			  				  else if ((10<=text)&&(text<15))
  			  				  {
								  $("#rudimg").css('background-image',"url(img/"+ temp+"/r10.png)");
							  }
  			  				  else if ((15<=text)&&(text<20))
							  {
								 $("#rudimg").css('background-image',"url(img/"+ temp+"/r15.png)"); 
							  }
  			  				 else if ((20<=text)&&(text<25))
							 {
								 $("#rudimg").css('background-image',"url(img/"+ temp+"/r20.png)");
							 }
  			  				else if ((25<=text)&&(text<30))
							{
								$("#rudimg").css('background-image',"url(img/"+ temp+"/r25.png)");
							} 
  			  				else if ((30<=text)&&(text<35))
							{
								$("#rudimg").css('background-image',"url(img/"+ temp+"/r30.png)");
							}
  			  				else if (35==text)
							{
								$("#rudimg").css('background-image',"url(img/"+ temp+"/r35.png)");
							}
  			  				 
  			  				
  			  							
  			  						 
  			}  
			
			else if (text == 0){
			  				
			  				
							document.getElementById('rudshow').innerText=PrefixZero(text, 2);
							document.getElementById("rudshow").style.color = "#FFFFFF";
			  				$("#rudimg").css('background-image',"url(img/"+ temp+"/0.png)");
			}  


 }
		 
function PrefixZero(num, n) {
    return (Array(n).join(0) + num).slice(-n);
}



		 
